package thread;
/**
 * 静态方法上使用synchronized后，那么该方法一定具有同步效果。
 */
public class SyncDemo3 {
    public static void main(String[] args) {
        Boo b1 = new Boo();
        Boo b2 = new Boo();
        Thread t1 = new Thread(){

            @Override
            public void run(){
                Boo.dosome();
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run(){
                Boo.dosome();
            }
        };
        t1.start();
        t2.start();
    }
}
class Boo{

    public static void dosome(){
        //静态方法的同步块中依然应当使用当前类的类对象作为锁对象
        synchronized(Boo.class) {
            try {
                Thread t = Thread.currentThread();
                System.out.println(t.getName() + ":正在执行dosome方法...");
                Thread.sleep(5000);
                System.out.println(t.getName() + ":执行dosome方法完毕!");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

